package com.exp.zhiwendemo;

import java.util.UUID;

import com.dnake.v710.MessageLayer;
import com.dnake.v710.MessageProcessor;
import com.exp.zhiwendemo.widget.Button2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class FirstActivity extends Activity implements MessageProcessor {
	
	public static FirstActivity handle = null;
	private int mMode = -1;  // -1-空闲  0-搜索模式   1-预录模式  9-录入状态 2-启动搜索  3-结束搜索  4-删除 5-清空 6-指纹下发 7-指纹数量
	private int mLoginTime = 0;  //录入次数
	private Boolean bSearch = false;  //指纹搜索服务运行标志
	private Boolean bWebView = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.first);

		handle = this;
		
		Button2 b2 = (Button2)findViewById(R.id.main_settings);
		b2.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(FirstActivity.this, SetupLabel.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(intent);
			}
		});
		
		Button b = (Button)findViewById(R.id.btn_login);
		b.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				mMode = 1;       //预录入模式：1、匹配可用序号 2、打开指纹灯
				mLoginTime = 1;
				MessageLayer.zfm_entry_login(); 
			}
		});
		
		b = (Button)findViewById(R.id.btn_start);
		b.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				mMode = 2;  // 打开指纹灯
				MessageLayer.zfm_led_onoff(1);
			}
		});
		
		b = (Button)findViewById(R.id.btn_stop);
		b.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				mMode = 3;  // 关闭指纹灯
				MessageLayer.zfm_led_onoff(0);
			}
		});
/*		
		b = (Button)findViewById(R.id.btn_delete);
		b.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {				
				if (CZJData.length == 512) {
					byte[] bytes = new byte[2048];
					int idx = 0;
					for (int i=0; i<512; i++) {
						bytes[idx++] = (byte) (((CZJData[i]>>4)&0x0F)|0x30);
						bytes[idx++] = (byte) ((CZJData[i]&0x0F)|0x30);
					}
					bytes[idx++] = 0x00;
					
					mMode = 4;
					MessageLayer.zfm_model_delete(0, 200, new String(bytes));
				}
			}
		});*/
		
		b = (Button)findViewById(R.id.btn_empty);
		b.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				mMode = 5;   //清空指纹模板
				MessageLayer.zfm_empty();
			}
		});
		
		b = (Button)findViewById(R.id.btn_download);
		b.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (CZJData.length == 512) {
					byte[] bytes = new byte[2048];
					int idx = 0;
					for (int i=0; i<512; i++) {
						bytes[idx++] = (byte) (((CZJData[i]>>4)&0x0F)|0x30);
						bytes[idx++] = (byte) ((CZJData[i]&0x0F)|0x30);
					}
					bytes[idx++] = 0x00;
					
					mMode = 6;   //指纹下发
					MessageLayer.zfm_issued(new String(bytes));
				}
			}
		});
		
		b = (Button)findViewById(R.id.btn_templateNum);
		b.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mMode = 7;
				MessageLayer.zfm_templateNum();
			}
		});
		
		b = (Button)findViewById(R.id.main_apps);
		b.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				WebView webview = (WebView)findViewById(R.id.webview);
				if (!bWebView) {
					webview.loadUrl("http://www.baidu.com/");
					webview.setVisibility(View.VISIBLE);
				} else {
					webview.clearView();
					webview.setVisibility(View.GONE);
				}
				bWebView = !bWebView;
			}
		});
		
		TextView main_uuid = (TextView)findViewById(R.id.main_uuid);
//		main_uuid.setText(getMyUUID());

		MessageLayer.start(this);
		MessageLayer.setMessageProcessor(this);
	}
	
	private String getMyUUID() {

	    final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(TELEPHONY_SERVICE);

	    final String tmDevice, tmSerial, tmPhone, androidId;

	    tmDevice = "" + tm.getDeviceId();

	    tmSerial = "" + tm.getSimSerialNumber();

	    androidId = "" + android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);

	    UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());

	    String uniqueId = deviceUuid.toString();

	    //Log.d("deviceuuid", "uuid=" + uniqueId);

	    return uniqueId;

	}
	
	private String getSetpDesc(String step) {
		String s = "";
		
		if (step.equals("ReadConList"))
			s = "分配储存位置";
		else if (step.equals("OpenLed"))
			s = "打开指纹灯";
		else if (step.equals("CloseLed"))
			s = "关闭指纹灯";
		else if (step.equals("GenImg"))
			s = "采集图像中";
		else if (step.equals("Img2Tz"))
			s = "图像转特征";
		else if (step.equals("RegModel"))
			s = "合成模板中";
		else if (step.equals("LoadChar"))
			s = "加载模板中";
		else if (step.equals("UpChar"))
			s = "上传特征或模板中";
		else if (step.equals("DownChar"))
			s = "下载特征或模板中";
		else if (step.equals("DelChar"))
			s = "删除模板中";
		else if (step.equals("Lssued"))
			s = "进入下载模式";
		else if (step.equals("Store"))
			s = "存储模板中";
		else if (step.equals("Match"))
			s = "指纹匹配中";
		else if (step.equals("Reset"))
			s = "指纹模块重置中";
		return s;
	}

	private char[] CZJData = {0x03, 0x01, 0x5d, 0x0a, 0x81, 0x00, 0xff, 0xfe, 0xff, 0xfe, 0xff, 0xfe, 0xfc, 0xfe, 0xf0, 0x1e, 0xe0, 0x0e, 0xe0, 0x06, 0xe0, 0x02, 0xc0, 0x02, 0xc0, 0x02, 0xc0, 0x02, 0xc0, 0x02, 0xc0, 0x02, 0xe0, 0x02, 0xe0, 0x06, 0xe0, 0x06, 0xe0, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x47, 0x13, 0x03, 0x5e, 0x2a, 0x96, 0x16, 0x7e, 0x59, 0x9e, 0x59, 0x9e, 0x5c, 0xaa, 0x9a, 0x1e, 0x3f, 0xb1, 0x57, 0xde, 0x5d, 0xb5, 0x45, 0x9e, 0x3d, 0xb8, 0x83, 0xbe, 0x5d, 0x42, 0xc7, 0x57, 0x45, 0x1a, 0x18, 0x9d, 0x46, 0x9d, 0x42, 0xdd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x01, 0x66, 0x11, 0x81, 0x00, 0xff, 0xfe, 0xff, 0xfe, 0xfc, 0x3e, 0xf0, 0x0e, 0xf0, 0x02, 0xe0, 0x00, 0xe0, 0x00, 0xe0, 0x00, 0xe0, 0x00, 0xe0, 0x00, 0xe0, 0x00, 0xe0, 0x00, 0xe0, 0x00, 0xf0, 0x00, 0xf0, 0x00, 0xf0, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x15, 0xd8, 0x1e, 0x59, 0x9e, 0x59, 0x9e, 0x72, 0xa8, 0x99, 0x1e, 0x5c, 0xaa, 0x9a, 0x1e, 0x75, 0xac, 0xc4, 0x7e, 0x3f, 0xb1, 0x98, 0x1e, 0x5d, 0xb5, 0x45, 0x7e, 0x74, 0x37, 0xc5, 0xde, 0x3d, 0x38, 0x43, 0xde, 0x6b, 0x42, 0x45, 0xfe, 0x6c, 0x19, 0x9a, 0x1f, 0x68, 0x26, 0xc3, 0x3f, 0x5c, 0xc2, 0x86, 0xd7, 0x4b, 0x10, 0x19, 0xbc, 0x47, 0x13, 0x03, 0x7c, 0x44, 0x9a, 0x19, 0x1d, 0x46, 0x9d, 0x42, 0xbd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
	private char[] mData = new char[768];  //存储指纹特征数据

	@Override
	public void processZfmResult(int code, int seq) {
		TextView tv = null;
		
		switch (mMode) {
		case 1:
		case 9:
			tv = (TextView)findViewById(R.id.tv_auto_login);
			break;
			
		case 2:
			tv = (TextView)findViewById(R.id.tv_start);
			break;
			
		case 3:
			tv = (TextView)findViewById(R.id.tv_stop);
			break;
			
		case 5:
			tv = (TextView)findViewById(R.id.tv_empty);
			break;
			
		case 6:
			tv = (TextView)findViewById(R.id.tv_download);
			break;
			
		case 7:
			tv = (TextView)findViewById(R.id.tv_templateNum);
			break;
			
		case 0:
			tv = (TextView)findViewById(R.id.tv_status);
			break;

		default:
			break;
		}
		
		if (tv == null) return;
		
		switch (code) {
		case 0x00:
		{	
			if (seq != -1) {
				if (mMode == 0)	tv.setText("搜索成功，请移开手指   所在序号："+seq);
				else if (mMode == 7) tv.setText(""+seq);
			} else {
				tv.setText("成功");
			}
			
			if (mMode == 1) {
				mMode = 9;
				mLoginTime = 1;
			} else if (mMode == 9) {
				if (mLoginTime++ >= 2)
					mMode = -1;  //退出录入模式
			}
			
			if (mMode == 2) {
				bSearch = true;
				mMode = 0;
			} else if (mMode == 3) {
				bSearch = false;
				mMode = -1;
			}
		}
			break;
			
		case 0x01:
			tv.setText("收包有错");
			break;
			
		case 0x02:
			tv.setText("传感器上无指纹");
			break;
			
		case 0x03:
			tv.setText("录入不成功");
			break;
			
		case 0x06:
			tv.setText("图像太乱，失败");
			break;
			
		case 0x07:
			tv.setText("特征点太少，失败");
			break;
			
		case 0x09:
			tv.setText("没搜索到");
			break;

		case 0x0a:
			tv.setText("合成失败(按指不是同一个手指)");
			break;
			
		case 0x0b:
			tv.setText("存储序号超过有效范围");
			break;
			
		case 0x0c:
			tv.setText("模板无效");
			break;
			
		case 0x0d:
			tv.setText("指令执行失败");
			break;
			
		case 0x0e:
			tv.setText("不能接收后续数据包");
			break;
			
		case 0x0f:
			tv.setText("不能发送后续数据包");
			break;
			
		case 0x10:
			tv.setText("删除模板失败");
			break;
			
		case 0x11:
			tv.setText("清空模板失败");
			break;
	
		case 0x15:
			tv.setText("图像缓冲区没有有效原始图而生不成图像");
			break;				
			
		case 0x18:
			tv.setText("读写模板出错");
			break;			
			
		case 0x56:
			tv.setText("第一次采集成功");
			break;
			
		case 0x57:
			tv.setText("第二次采集成功");
			break;
			
		case 0x24:
			tv.setText("因重复登记而失败");
			break;
			
		case 0xFF:
			tv.setText("终止");
			break;
			
		default:
			break;
		}
	}

	@Override
	public void processZfmStep(String step) {
		TextView tv = null;
		
		switch (mMode) {
		case 1:
		case 9:
			tv = (TextView)findViewById(R.id.tv_auto_login);
			break;
			
		case 2:
			tv = (TextView)findViewById(R.id.tv_start);
			break;
			
		case 3:
			tv = (TextView)findViewById(R.id.tv_stop);
			break;
			
		case 5:
			tv = (TextView)findViewById(R.id.tv_empty);
			break;
			
		case 6:
			tv = (TextView)findViewById(R.id.tv_download);
			break;
			
		case 0:
			tv = (TextView)findViewById(R.id.tv_status);
			break;

		default:
			break;
		}
		
		if (tv == null) return;
		
		tv.setText(getSetpDesc(step));
	}

	@Override
	public void processZfmFinger() {
		if (mMode == 9) {		
			MessageLayer.zfm_login(mLoginTime, mLoginTime == 2 ? true : false);	
		} else {			
			if (bSearch) {
				MessageLayer.zfm_search(0, 999, true);
			}
		}
	}

	@Override
	public void processZfmData(String d) {
		boolean match = false;
		if (!d.isEmpty()) {
			byte[] b = d.getBytes();
			if (b.length == 1024 || b.length == 1536) {
				int idx = 0;
				match = true;
				for (int i=0; i<b.length; i+=2) {
					mData[idx++] = (char) (((b[i]<<4)&0xF0)+(b[i+1]&0x0F));
					System.out.format("0x%02x, ", (short)mData[idx-1]);
					if (mMode == -1) {
						CZJData[idx-1] = mData[idx-1];
					} else {
						if (CZJData[idx-1] != mData[idx-1])
							match = false;
					}
				}
				System.out.print('\n');
			}
		}
		if (mMode == 0) {
			if (match) {
				Toast.makeText(this, "匹配成功", Toast.LENGTH_SHORT).show();;
			} else {
				Toast.makeText(this, "匹配失败", Toast.LENGTH_SHORT).show();
			}
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK)
			return false;	
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void processCardData(String data) {
		// TODO Auto-generated method stub
		TextView tv = (TextView)findViewById(R.id.tv_brush_card);
		tv.setText(data);
	}
}
